package com.dbn.webmagic.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dbn.webmagic.entity.NoticeList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Http请求
 * @author lhp
 */
public class AhttpRequest {
    public static List<NoticeList> r(String url, int flag){
        List<NoticeList> list=new ArrayList<>();
        String sel1="";//申请公告；
        String sel2="";//植物种类
        String name="郑单958";//品种名称
        //查询接口返回json
        String url1 = "http://www.nybkjfzzx.cn/Handler/DataSelectHandler.ashx?op=getcondition&idx=&per=&sel1="+sel1+"%E5%85%A8%E9%83%A8&sel2="+sel2+"&sel3=&sel4=&t1=&t2=&t3=&t4=&pgs=15&nid=0&name="+name+"&ad=&_=1635923968459";
        String json = requestUrl(url1);
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            //处理返回的数据
            for (int i = 0; i <jsonArray.size(); i++) {
                JSONObject jsonArrayObjectItem = JSONObject.parseObject(jsonArray.get(i).toString());
                if(jsonArrayObjectItem!=null){
                    NoticeList noticeList=new NoticeList();
                    String id=jsonArrayObjectItem.getString("id");
                    String uuid = UUID.randomUUID().toString().replaceAll("-","").substring(0,19);
                    noticeList.setUuid(uuid);
                    noticeList.setNoticeSimpleId(id);
                    if(flag==0){//综合查询
                        String url2=url+jsonArrayObjectItem.getString("applyno")+"";
                        noticeList.setUrl(url2);
                        list.add(noticeList);
                    }if(flag==1){//公告查询
                        String url2=url+jsonArrayObjectItem.getString("t")+"&an="+jsonArrayObjectItem.getString("id")+"";
                        noticeList.setUrl(url2);
                        list.add(noticeList);
                    }
//                    notice.setId(jsonArrayObjectItem.getString("id"));
//                    notice.setApplyno(jsonArrayObjectItem.getString("applyno"));//申请号
//                    notice.setT(jsonArrayObjectItem.getString("t"));

                    //System.out.println("http://www.nybkjfzzx.cn/p_pzbh/sub_ggxq.aspx?an="+applyno+"");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    public static String requestUrl(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream(), "utf-8"));//防止乱码
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }
}